﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Nop.Admin.Models.Catalog;
using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Stores;
using Nop.Services.Catalog;
using Nop.Services.Customers;
using Nop.Services.Helpers;
using Nop.Services.Localization;
using Nop.Services.Security;
using Nop.Services.Stores;
using Nop.Web.Framework;
using Nop.Web.Framework.Controllers;
using Telerik.Web.Mvc;


namespace Nop.Admin.Controllers
{
    [AdminAuthorize]
    public partial class ProductReviewController : BaseNopController
    {
        #region Fields

        private readonly ICustomerContentService _customerContentService;
        private readonly IProductService _productService;
        private readonly IDateTimeHelper _dateTimeHelper;
        private readonly ILocalizationService _localizationService;
        private readonly IPermissionService _permissionService;
        private readonly IStoreService _storeService;
        private readonly IWorkContext _workContext;

        #endregion Fields

        #region Constructors

        public ProductReviewController(ICustomerContentService customerContentService,
            IProductService productService, IDateTimeHelper dateTimeHelper,
            ILocalizationService localizationService, IPermissionService permissionService,
            IStoreService storeService, IWorkContext workContext)
        {
            this._customerContentService = customerContentService;
            this._productService = productService;
            this._dateTimeHelper = dateTimeHelper;
            this._localizationService = localizationService;
            this._permissionService = permissionService;
            this._storeService = storeService;
            this._workContext = workContext;
        }

        #endregion

        #region Utilities

        [NonAction]
        protected void PrepareProductReviewModel(ProductReviewModel model,
            ProductReview productReview, bool excludeProperties, bool formatReviewText)
        {
            if (model == null)
                throw new ArgumentNullException("model");

            if (productReview == null)
                throw new ArgumentNullException("productReview");

            model.Id = productReview.Id;
            model.ProductId = productReview.ProductId;
            model.ProductName = productReview.Product.Name;
            model.CustomerId = productReview.CustomerId;
            model.IpAddress = productReview.IpAddress;
            model.Rating = productReview.Rating;
            model.CreatedOn = _dateTimeHelper.ConvertToUserTime(productReview.CreatedOnUtc, DateTimeKind.Utc);
            if (!excludeProperties)
            {
                model.Title = productReview.Title;
                if (formatReviewText)
                    model.ReviewText = Core.Html.HtmlHelper.FormatText(productReview.ReviewText, false, true, false, false, false, false);
                else
                    model.ReviewText = productReview.ReviewText;
                model.IsApproved = productReview.IsApproved;
            }
        }

        #endregion

        #region Methods

        //list
        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult List()
        {
            bool isStoreAdmin, canManageAllStore, isAccessDenied;
            _permissionService.SecurityAccessForStoreAdmin(out isStoreAdmin, out canManageAllStore, out isAccessDenied);
            if (isAccessDenied)
                return AccessDeniedView();

            var model = new ProductReviewListModel();
            model.CanViewCustomer = _permissionService.Authorize(StandardPermissionProvider.ManageCustomers);

            return View(model);
        }

        [HttpPost, GridAction(EnableCustomBinding = true)]
        public ActionResult List(GridCommand command, ProductReviewListModel model)
        {
            bool isStoreAdmin, canManageAllStore, isAccessDenied;
            _permissionService.SecurityAccessForStoreAdmin(out isStoreAdmin, out canManageAllStore, out isAccessDenied);
            if (isAccessDenied)
                return Content("Access denied.");

            DateTime? createdOnFromValue = (model.CreatedOnFrom == null) ? null
                            : (DateTime?)_dateTimeHelper.ConvertToUtcTime(model.CreatedOnFrom.Value, _dateTimeHelper.CurrentTimeZone);

            DateTime? createdToFromValue = (model.CreatedOnTo == null) ? null
                            : (DateTime?)_dateTimeHelper.ConvertToUtcTime(model.CreatedOnTo.Value, _dateTimeHelper.CurrentTimeZone).AddDays(1);

            var productReviews = _customerContentService.GetAllCustomerContent<ProductReview>(0, null,
                createdOnFromValue, createdToFromValue);

            if (!canManageAllStore)
            {
                var productReviewList = new List<ProductReview>();
                // TBD: Right now, there is only generic method GetAllCustomerContent<T> above to get ProductReviews.
                //      The method operates on base class CustomerContent, and thus cannot be modified to filter by store.
                //      So, had to filter by store after the method above returns a list.
                //      Any other way to make this more efficient?? Create a service method that operates on ProductReview class??
                var stores = _storeService.GetAllStores(false, false, _workContext.CurrentCustomer.Id);
                foreach (var store in stores)
                {
                    productReviewList.AddRange(productReviews.Where(x => x.Product.StoreId == store.Id));
                }
                productReviews = productReviewList;
            }
            var gridModel = new GridModel<ProductReviewModel>
            {
                Data = productReviews.PagedForCommand(command).Select(x =>
                {
                    var m = new ProductReviewModel();
                    PrepareProductReviewModel(m, x, false, true);
                    return m;
                }),
                Total = productReviews.Count,
            };
            return new JsonResult
            {
                Data = gridModel
            };
        }

        //edit
        public ActionResult Edit(int id)
        {
            var customer = _workContext.CurrentCustomer;
            bool isStoreAdmin, canManageAllStore, isAccessDenied;
            _permissionService.SecurityAccessForStoreAdmin(out isStoreAdmin, out canManageAllStore, out isAccessDenied);
            if (isAccessDenied)
                return AccessDeniedView();

            var productReview = _customerContentService.GetCustomerContentById(id) as ProductReview;
            if (productReview == null)
                //No product review found with the specified id
                return RedirectToAction("List");

            var store = _storeService.GetStoreById(productReview.Product.StoreId);
            if (store == null)
            {
                throw new ArgumentNullException("store");
            }
            if (!canManageAllStore && store.AdminCustomerId != customer.Id)
            {
                return AccessDeniedView();
            }

            var model = new ProductReviewModel();
            PrepareProductReviewModel(model, productReview, false, false);
            model.CustomerAdmin = _permissionService.Authorize(StandardPermissionProvider.ManageCustomers);
            return View(model);
        }

        [HttpPost, ParameterBasedOnFormNameAttribute("save-continue", "continueEditing")]
        public ActionResult Edit(ProductReviewModel model, bool continueEditing)
        {
            var customer = _workContext.CurrentCustomer;
            bool isStoreAdmin, canManageAllStore, isAccessDenied;
            _permissionService.SecurityAccessForStoreAdmin(out isStoreAdmin, out canManageAllStore, out isAccessDenied);
            if (isAccessDenied)
                return AccessDeniedView();

            var productReview = _customerContentService.GetCustomerContentById(model.Id) as ProductReview;
            if (productReview == null)
                //No product review found with the specified id
                return RedirectToAction("List");
            var store = _storeService.GetStoreById(productReview.Product.StoreId);
            if (store == null)
            {
                ErrorNotification(_localizationService.GetResource("Admin.ProductReview.NoStore"));
            }
            if (!canManageAllStore && store.AdminCustomerId != customer.Id)
            {
                return AccessDeniedView();
            }

            if (ModelState.IsValid)
            {
                productReview.Title = model.Title;
                productReview.ReviewText = model.ReviewText;
                productReview.IsApproved = model.IsApproved;
                productReview.UpdatedOnUtc = DateTime.UtcNow;
                _customerContentService.UpdateCustomerContent(productReview);

                //update product totals
                _productService.UpdateProductReviewTotals(productReview.Product);

                //update store totals
                _storeService.UpdateProductReviewTotals(productReview.Product.StoreId);

                SuccessNotification(_localizationService.GetResource("Admin.Catalog.ProductReviews.Updated"));
                return continueEditing ? RedirectToAction("Edit", productReview.Id) : RedirectToAction("List");
            }


            //If we got this far, something failed, redisplay form
            PrepareProductReviewModel(model, productReview, true, false);
            return View(model);
        }

        //delete
        [HttpPost]
        public ActionResult Delete(int id)
        {
            var customer = _workContext.CurrentCustomer;
            bool isStoreAdmin, canManageAllStore, isAccessDenied;
            _permissionService.SecurityAccessForStoreAdmin(out isStoreAdmin, out canManageAllStore, out isAccessDenied);
            if (isAccessDenied)
                return AccessDeniedView();

            var productReview = _customerContentService.GetCustomerContentById(id) as ProductReview;
            if (productReview == null)
                //No product review found with the specified id
                return RedirectToAction("List");

            var product = productReview.Product;
            var store = _storeService.GetStoreById(product.StoreId);
            if (store == null)
            {
                ErrorNotification(_localizationService.GetResource("Admin.ProductReview.NoStore"));
            }
            if (!canManageAllStore && store.AdminCustomerId != customer.Id)
            {
                return AccessDeniedView();
            }
            _customerContentService.DeleteCustomerContent(productReview);
            //update product totals
            _productService.UpdateProductReviewTotals(product);

            //update store totals
            _storeService.UpdateProductReviewTotals(store.Id);

            SuccessNotification(_localizationService.GetResource("Admin.Catalog.ProductReviews.Deleted"));
            return RedirectToAction("List");
        }

        [HttpPost]
        public ActionResult ApproveSelected(ICollection<int> selectedIds)
        {
            var customer = _workContext.CurrentCustomer;
            bool isStoreAdmin, canManageAllStore, isAccessDenied;
            _permissionService.SecurityAccessForStoreAdmin(out isStoreAdmin, out canManageAllStore, out isAccessDenied);
            if (isAccessDenied)
                return AccessDeniedView();

            if (selectedIds != null)
            {
                foreach (var id in selectedIds)
                {
                    var productReview = _customerContentService.GetCustomerContentById(id) as ProductReview;
                    if (productReview != null)
                    {
                        var storeId = productReview.Product.StoreId;
                        var store = _storeService.GetStoreById(storeId);
                        if (store == null)
                        {
                            ErrorNotification(_localizationService.GetResource("Admin.ProductReview.NoStore"));
                            return RedirectToAction("List");
                        }
                        if (!canManageAllStore && store.AdminCustomerId != customer.Id)
                        {
                            return AccessDeniedView();
                        }
                        productReview.IsApproved = true;
                        _customerContentService.UpdateCustomerContent(productReview);
                        //update product totals
                        _productService.UpdateProductReviewTotals(productReview.Product);

                        //update store totals
                        _storeService.UpdateProductReviewTotals(productReview.Product.StoreId);
                    }
                }
            }

            return Json(new { Result = true });
        }

        [HttpPost]
        public ActionResult DisapproveSelected(ICollection<int> selectedIds)
        {
            var customer = _workContext.CurrentCustomer;
            bool isStoreAdmin, canManageAllStore, isAccessDenied;
            _permissionService.SecurityAccessForStoreAdmin(out isStoreAdmin, out canManageAllStore, out isAccessDenied);
            if (isAccessDenied)
                return AccessDeniedView();

            if (selectedIds != null)
            {
                foreach (var id in selectedIds)
                {
                    var productReview = _customerContentService.GetCustomerContentById(id) as ProductReview;
                    if (productReview != null)
                    {
                        var storeId = productReview.Product.StoreId;
                        var store = _storeService.GetStoreById(storeId);
                        if (store == null)
                        {
                            ErrorNotification(_localizationService.GetResource("Admin.ProductReview.NoStore"));
                            return RedirectToAction("List");
                        }
                        if (!canManageAllStore && store.AdminCustomerId != customer.Id)
                        {
                            return AccessDeniedView();
                        }
                        productReview.IsApproved = false;
                        _customerContentService.UpdateCustomerContent(productReview);
                        //update product totals
                        _productService.UpdateProductReviewTotals(productReview.Product);
                        //update store totals
                        _storeService.UpdateProductReviewTotals(productReview.Product.StoreId);
                    }
                }
            }

            return Json(new { Result = true });
        }




        #endregion
    }
}
